package com.wenxixuanwu.fruitstore.controller;

import com.wenxixuanwu.fruitstore.Vo.CommentVo;
import com.wenxixuanwu.fruitstore.entity.Code;
import com.wenxixuanwu.fruitstore.entity.User;
import com.wenxixuanwu.fruitstore.mapper.CommentMapper;
import com.wenxixuanwu.fruitstore.mapper.LogBookMapper;
import com.wenxixuanwu.fruitstore.mapper.UserMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class CenterController {

    @Autowired
    UserMapper mapper;
    @Autowired
    LogBookMapper logBookMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @GetMapping("/center/select")
    public User centerSelect(HttpSession session) {
        User user = (User) session.getAttribute("u");
        return user;
    }

    @PostMapping("/center/login")
    public int centerLogin(@RequestBody User user, HttpSession session) {
        User users = mapper.getByUserName(user.getUsername());

        if (users != null && users.getPassword().equals(DigestUtils.md5Hex(user.getPassword()))) {
            Cookie c1 = new Cookie("username", users.getUsername());
            Cookie c2 = new Cookie("password", users.getPassword());
            c1.setMaxAge(60 * 60);
            c2.setMaxAge(60 * 60);
            session.removeAttribute("u");
            session.setAttribute("u", users);
            return 1;
        } else {
            return 0;
        }
    }

    @PostMapping("/center/update")
    public void centerUpdate(@RequestBody User user, HttpSession session) {
        User users = (User) session.getAttribute("u");
        user.setId(users.getId());
        mapper.updateCenter(user.getUsername(), user.getNick(), user.getSex(), user.getAddress(), user.getPhone(), user.getId());
        session.removeAttribute("u");
        users = mapper.getByUserName(user.getUsername());
        session.setAttribute("u", users);
    }

    @PostMapping("/center/password")
    public int centerPassword(@RequestBody Code code, HttpSession session) {
        System.out.println(code);
        User user = (User) session.getAttribute("u");
        if (DigestUtils.md5Hex(code.getPassword()).equals(user.getPassword())) {
            mapper.updatePassword(DigestUtils.md5Hex(code.getNewpassword()), user.getId());
            session.removeAttribute("u");
            user = mapper.getByUserName(user.getUsername());
            session.setAttribute("u", user);
            return 1;
        } else {
            return 0;

        }
    }

    @GetMapping("/center/selectcomment")
    public List<CommentVo> SelectUserId(HttpSession session) {
        User user = ((User) session.getAttribute("u"));
        int userId;
        if (user == null) {
            return null;
        }
        userId = user.getId();
        System.out.println(commentMapper.getComments(logBookMapper.selectUserId(userId)));
        return commentMapper.getComments(logBookMapper.selectUserId(userId));
    }

    @GetMapping("/center/selectmycomment")
    public List<CommentVo> centerSelectComment(HttpSession session) {
        User user = ((User) session.getAttribute("u"));
        int userId;
        if (user == null) {
            return null;
        }
        userId = user.getId();
        List<CommentVo> list = commentMapper.getCommentById(userId);
        List<Integer> ls = new ArrayList<>();
        for (CommentVo c : list) {
            ls.add(c.getId());
        }
        return list;
    }

    @GetMapping("/center/selectUser")
    public List<User> centerSelectUser(int id){
    return commentMapper.getCommentId(commentMapper.getUserId(id));
    }


}
